package ${basePackage}.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${basePackage}.entity.${entityName};
import ${basePackage}.dto.${entityName}DTO;
import ${basePackage}.mapper.${entityName}Mapper;
import ${basePackage}.service.${entityName}Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
* 服务实现层
* ${author}
* ${now}
*/
@Service
public class ${entityName}ServiceImpl extends ServiceImpl<${entityName}Mapper, ${entityName}> implements ${entityName}Service {


    @Resource
    private ${entityName}Mapper ${entityName?uncap_first}Mapper;


    @Override
    public ResultDTO<List<${entityName}DTO>> selectListPage(${entityName}PageQueryDTO dto) {
        LambdaQueryChainWrapper<${entityName}> queryChainWrapper = this.lambdaQuery();
        // 以下写条件查询，mybatis plus的使用见readme文档
        //   if (StrUtil.isNotEmpty(dto.getName())) {
        //        queryChainWrapper = queryChainWrapper.like(${entityName}::getName, dto.getName());
        //   }
        queryChainWrapper.orderByDesc(${entityName}::getCreateTime);
        IPage<${entityName}> page = queryChainWrapper.page(dto);
        List<${entityName}> entityList = page.getRecords();
        // 以下写entity to dto的转换规则，可在对应的mapStruct中直接写，mapStruct的使用见readme文档
        List<${entityName}DTO> dtoList = ${entityName}Struct.INSTANCE.toDTO(entityList);
        return ResultDTO.ok(page.getTotal(), dtoList);
    }

    @Override
    public List<${entityName}DTO> selectList(${entityName}DTO dto){
        // 以下写条件查询，mybatis plus的使用见readme文档
        List<${entityName}> list = this.lambdaQuery()
                .eq(Objects.nonNull(dto.getId()), ${entityName}::getId, dto.getId()).list();
        // 以下写entity to dto的转换规则，可在对应的mapStruct中直接写，mapStruct的使用见readme文档
        return ${entityName}Struct.INSTANCE.toDTO(list);
    }

    @Override
    public ${entityName}DTO save(${entityName}DTO dto) {
        if (Objects.isNull(dto.getId()) || dto.getId() == 0){
            // 新增
            // 以下写校验规则和其他业务逻辑，校验规则建议使用项目中已封装好的AssertUtils，前端的入参校验建议使用validation注解
            ${entityName} entity = ${entityName}Struct.INSTANCE.toEntity(dto);
            this.save(entity);
        }else{
            // 修改
            getAndCheck(dto);
            // 以下写校验规则和其他业务逻辑，校验规则建议使用项目中已封装好的AssertUtils，前端的入参校验建议使用validation注解
            ${entityName} entity = ${entityName}Struct.INSTANCE.toEntity(dto);
            this.updateById(entity);
        }
        return dto;
    }

    @Override
    public ${entityName}DTO delete(${entityName}DTO dto) {
        ${entityName} entity = getAndCheck(dto);
        // 以下写校验规则和其他业务逻辑，校验规则建议使用项目中已封装好的AssertUtils，前端的入参校验建议使用validation注解
        this.removeById(entity.getId());
        return ${entityName}Struct.INSTANCE.toDTO(entity);
    }

    private ${entityName} getAndCheck(${entityName}DTO dto) {
        AssertUtils.isTrue(Objects.nonNull(dto.getId()) && dto.getId() != 0, "主键不能为空！");
        return Optional.ofNullable(this.getById(dto.getId())).orElseThrow(() -> new BusinessException("数据不存在或已删除，请检查！"));
    }
}
